/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cinema.client.cinemaclient.ui;

import com.cinema.client.cinemaclient.managers.MovieManager;
import com.cinema.client.cinemaclient.managers.UserManager;
import com.cinema.client.cinemaclient.model.Movie;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Zsolt Szecsi
 */
public class MovieDetailFrame extends JFrame {

    private final static String[] ROLES = {"user", "employee"};
    private JTextField titleTxt, directorTxt, yearTxt;
    private JButton okBtn, cancelBtn;
    private MovieManager movieManager;
    private JFrame parent;
    private Movie movie;

    public MovieDetailFrame(JFrame parent, Movie movie) {
        super("Movie");
        // init simple attributes    
        this.movie = movie;
        movieManager = new MovieManager();
        this.parent = parent;
        // init ui elements
        JPanel mainPanel = new JPanel();
        JLabel titleLbl = new JLabel("Title:");
        JLabel directorLbl = new JLabel("Director:");
        JLabel yearLbl = new JLabel("Year:");
        if (movie != null) {
            titleTxt = new JTextField(movie.getTitle());
            directorTxt = new JTextField(movie.getDirector());
            yearTxt = new JTextField("" + movie.getYear());
        } else {
            titleTxt = new JTextField();
            directorTxt = new JTextField();
            yearTxt = new JTextField();
        }
        okBtn = new JButton("Ok");
        cancelBtn = new JButton("Cancel");

        //compose ui
        mainPanel.setLayout(new GridLayout(4, 2));
        mainPanel.add(titleLbl);
        mainPanel.add(titleTxt);
        mainPanel.add(directorLbl);
        mainPanel.add(directorTxt);
        mainPanel.add(yearLbl);
        mainPanel.add(yearTxt);
        mainPanel.add(okBtn);
        mainPanel.add(cancelBtn);

        this.setContentPane(mainPanel);
        this.setBounds(100, 100, 300, 150);
        setListeners();
    }

    private void setListeners() {
        okBtn.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                System.out.println("Click listener called");
                if ("".equals(titleTxt.getText()) || "".equals(directorTxt.getText()) || "".equals(yearTxt.getText())) {
                    System.out.println("Click listener error1");
                    JOptionPane.showMessageDialog(MovieDetailFrame.this, "Error: Empty field!");
                    return;
                }
                boolean isNumber = true;
                try {
                    int year = Integer.parseInt(yearTxt.getText());
                } catch (Exception ex) {
                    isNumber = false;
                }

                if (!isNumber) {
                    System.out.println("Click listener error2");
                    JOptionPane.showMessageDialog(MovieDetailFrame.this, "Error: yaer is not a number!");
                    return;
                }
                System.out.println("Click listener ok");
                if (movie == null) {
                    movie = new Movie();
                }

                movie.setTitle(titleTxt.getText());
                movie.setDirector(directorTxt.getText());
                movie.setYear(Integer.parseInt(yearTxt.getText()));
                System.out.println("Movie id is: " + movie.getId());
                if (movie.getId() == 0) {
                    movieManager.insertMovie(movie);
                } else {
                    movieManager.updateMovie(movie);
                }
                parent.setVisible(true);
                ((MovieFrame) parent).refresh();
                MovieDetailFrame.this.dispose();
            }
        });
        cancelBtn.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                parent.setVisible(true);
                MovieDetailFrame.this.dispose();
            }
        });

    }

}
